#!usr/bin/env python 
#-*- coding:utf-8 -*-
#PyQt5的对话框系列

import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QLabel,QInputDialog,QTextEdit,QPushButton,QTextBrowser,QFileDialog,QColorDialog,QFontDialog,QProgressDialog,QLineEdit,QMessageBox
from PyQt5.QtCore import QT_VERSION_STR
from PyQt5.Qt import QT_VERSION,QColor,QTextCharFormat,QTextCursor,Qt


class DialogWin(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(350,200,500,400)
        self.setWindowTitle("对话框系列")
        #self.initInputDialog()
        #self.initDialog()
        self.initProgress()

    def initProgress(self): #进度条对话框
        self.lb = QLabel("文件数量",self)
        self.lb.move(20,40)

        self.bt1 = QPushButton('开始',self)
        self.bt1.move(20,80)

        self.edit = QLineEdit('100000',self)
        self.edit.move(100,40)
        self.bt1.clicked.connect(self.showDialog)
    def showDialog(self):
        num = int(self.edit.text())
        dialogProgress = QProgressDialog(self)
        dialogProgress.setWindowTitle("请稍等")
        dialogProgress.setLabelText("正在操作...")
        dialogProgress.setCancelButtonText("取消")
        dialogProgress.setMinimumDuration(5) 
        '''[如果任务的预期持续时间小于minimumDuration，
        则对话框根本不会出现。这样可以防止弹出对话框，
        快速完成任务。对于预期超过minimumDuration的任务，
        对话框将在minimumDuration时间之后或任何进度设置后立即弹出。
        如果设置为0，则只要设置任何进度，将始终显示对话框。 默认值为4000毫秒,即4秒。]
        '''

        dialogProgress.setWindowModality(Qt.WindowModal) #设置模态
        dialogProgress.setRange(0,num)
        for i in range(0,num+1):
            dialogProgress.setValue(i)
            if dialogProgress.wasCanceled():
                QMessageBox.warning(self,"警告","操作失败")
                break
        else:
            QMessageBox.information(self,"提示","操作成功")

    def initInputDialog(self): #输入对话框
        px = 20
        py = 20
        lbl = self.createLbl("姓名：",px,py)
        print(lbl.width(),lbl.height())
        
        print(lbl.width(),lbl.height())
        self.lblName = self.createLbl("张三",lbl.width()+px,+py)

        self.btnName = QPushButton("修改姓名",self)
        self.btnName.move(self.lblName.x()+self.lblName.width()+10,py)
        
        self.btnHtml = QPushButton("测试html文本",self)
        self.btnHtml.move(self.btnName.x(),self.btnName.y()+self.btnName.height()+10)
        self.htmlText = QTextBrowser(self)
        self.htmlText.resize(300,150)
        self.htmlText.move(px,self.btnHtml.y()+self.btnHtml.height() )

        self.btnName.clicked.connect(self.clickBtn)
        self.btnHtml.clicked.connect(self.clickBtn)

    def initDialog(self):
        self.textArea = QTextEdit(self)
        self.textArea.move(20,20)
        self.textArea.resize(300,200)

        btnX = self.textArea.x()+self.textArea.width()
        self.btnFile=QPushButton("打开文件",self)
        self.btnFile.move(btnX,self.textArea.y())

        self.btnClr = QPushButton("颜色",self)
        self.btnClr.move(btnX,self.btnFile.y()+self.btnFile.height())

        self.btnFont = QPushButton("字体",self)
        self.btnFont.move(btnX,self.btnClr.y()+self.btnClr.height())

        self.btnFile.clicked.connect(self.clickBtn2)
        self.btnClr.clicked.connect(self.clickBtn2)
        self.btnFont.clicked.connect(self.clickBtn2)
        
    def createLbl(self,text,px,py):
        lbl = QLabel(text,self)
        #lbl.adjustSize() #自动宽高
        lbl.move(px,py)
        return lbl 

    def clickBtn(self,e):
        send = self.sender()
        if send==self.btnName:
            #text,ok=QInputDialog.getText(self,"title->输入姓名","请输入姓名:")
            #text,ok=QInputDialog.getInt(self,"年龄","请输入年龄",min=1,max=200) #相当于弹出一个有number input功能的对话框 ,getDouble方法功能类似
            #text,ok=QInputDialog.getItem(self,"性别","请选中性别：",["男","女"]) #相当于弹出一个有下拉列表功能的对话框
            print(text,ok)
            if ok:
                self.lblName.setText(str(text))
        elif send==self.btnHtml:
            print("toPlainText",self.htmlText.toPlainText())
            print("toHtml",self.htmlText.toHtml())
            text,ok=QInputDialog.getMultiLineText(self,"简介","请输入简介：",text=self.htmlText.toPlainText())#相当于弹出一个有可以输入html标记的文本对话框
            print(text,ok)
            if ok:
                self.htmlText.setText(text)
        

    def clickBtn2(self):
        send = self.sender()
        if send==self.btnFile:
            files = QFileDialog.getOpenFileName(self,"选择打开的文件","./") #打开单个文件
            #files = QFileDialog.getOpenFileNames(self,"选择打开的文件","./") #打开多个文件
            url = files[0]
            if url:
                with open(url,"r",encoding="utf-8") as f:
                    self.textArea.setText(f.read())

        elif send==self.btnClr:
            clr = QColorDialog.getColor()
            print("clr",clr)
            if clr.isValid():
                self.textArea.setTextColor(clr)
            qClr = QColor(255,0,0)
            '''
            self.textArea.selectAll()                    
            self.textArea.setTextColor(qClr) #只能设置当前选中的文本
            self.textArea.moveCursor(QTextCursor.End)  
            

            fmt = QTextCharFormat()
            fmt.setForeground(clr)
            #self.textArea.moveCursor(QTextCursor.Start)
            cursor =self.textArea.textCursor() #获取当前光标
            cursor.mergeCharFormat(fmt)               
            self.textArea.mergeCurrentCharFormat(fmt)            
            self.textArea.moveCursor(QTextCursor.End)
            '''
        elif send==self.btnFont:
            pass

if __name__=="__main__":
    app = QApplication(sys.argv)
    win = DialogWin()
    win.show()    
    print("QT_VERSION_STR",QT_VERSION_STR)
    print("QT_VERSION",QT_VERSION)
    sys.exit(app.exec())
